﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using ProjectBase.Core;
using ProjectBase.Mvc;

using $prjsn$.Common;
using $prjsn$.Common.Business.Services;

namespace $safeprojectname$.Business.Security
{
    [TestClass]
    public class SecurityTest : EntityTestBase<IStoryNodeFacade>
    {
        [TestMethod]
        public void GetRoles_ForTESTLOGIN_Passed()
        {
            var roles = SecurityManager.GetRolesForUser("TESTLOGIN");
            Assert.IsTrue(roles.IsNotEmpty());
        }

        [TestMethod]
        public void GetRoles_ForCurrentUser_Passed()
        {
            var roles = SecurityManager.GetRolesForUser();
            Assert.IsTrue(roles.IsNotEmpty());
        }

        [TestMethod]
        public void Get_GrantedControllers_ForCurrentUser_Passed()
        {
            var controllers = SecurityManager.GrantedControllers;
            Assert.IsTrue(controllers.IsNotEmpty());
            Assert.IsTrue(controllers.Any(c => c.ControllerName.IsEqual(Str.Controllers.Home)));
            Assert.IsFalse(controllers.Any(c => c.ControllerName.IsEqual(Str.Controllers.User)));
        }

        #region Identity
        class TestPrincipal : IPrincipal
        {
            readonly IIdentity _identity;
            public TestPrincipal()
            {
                _identity = new TestIdentity();
            }

            public IIdentity Identity
            {
                get { return _identity; }
            }

            public bool IsInRole(string role)
            {
                return false;
            }
        }
        class TestIdentity : IIdentity
        {
            public string AuthenticationType
            {
                get { return "Windows"; }
            }

            public bool IsAuthenticated
            {
                get { return true; }
            }

            public string Name
            {
                get { return "TESTLOGIN"; }
            }
        }
        #endregion Identity

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        [TestInitialize]
        public override void MyTestInitialize()
        {
            base.MyTestInitialize(); // Use TestInitialize to run code before running each test
            Thread.CurrentPrincipal = new TestPrincipal();
        }

        [TestCleanup]
        public override void MyTestCleanup()
        {
            base.MyTestCleanup(); // Use TestCleanup to run code after each test has run
        }
        #endregion
    }
}
